import { get } from '../../utils/request';

Page({
  data: {
    orderId: null,
    orderDetail: null,
    statusMap: {
      pending: '待支付',
      processing: '进行中',
      completed: '已完成',
      cancelled: '已取消'
    },
    timer: null
  },
  onLoad(options) {
    if (options.orderId) {
      this.setData({ orderId: options.orderId });
      this.fetchOrderDetail(options.orderId);
      // 启动定时器，每5秒刷新一次订单状态
      const timer = setInterval(() => {
        this.fetchOrderDetail(this.data.orderId);
      }, 5000);
      this.setData({ timer });
    }
  },
  onUnload() {
    // 页面卸载时清除定时器
    if (this.data.timer) {
      clearInterval(this.data.timer);
      this.setData({ timer: null });
    }
  },
  fetchOrderDetail(orderId) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    get(`/api/orders/${orderId}`)
      .then(res => {
        wx.hideLoading();
        this.setData({ orderDetail: res });
      })
      .catch(err => {
        wx.hideLoading();
        wx.showToast({
          title: err.message || '获取订单详情失败',
          icon: 'none'
        });
        console.error('获取订单详情失败:', err);
      });
  }
}); 